package cn.aishua.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.aishua.util.SharedPrefConst;

import cn.aishua.R;

public class AbstractSlideMenuActivity
    extends Activity
{

    protected LinearLayout llMenu;
    protected LinearLayout llMain;
    protected int WIDTH; //width of menu
    protected int p1 = 0; // x position of touch down
    protected int p2 = 0; // x position of current
    protected int d = 0; // x distance between p1 and p2

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.slide_menu );
        initiate();

    }

    private void initiate()
    {
        llMenu = (LinearLayout) findViewById( R.id.slide_menu );
        llMain = (LinearLayout) findViewById( R.id.main_content );
        WIDTH = llMenu.getLayoutParams().width;

        AddItemsIntoMenu();
    }

    protected void AddItemsIntoMenu()
    {
        //        llMenu.addView( createView( R.layout.navi_tab_ind_search_bar, llMenu ) );
        llMenu.addView( createIndicatorViewLayer1( "热门推荐" ) );
        llMenu.addView( createIndicatorViewLayer1( "查找商户" ) );
        llMenu.addView( createIndicatorViewLayer1( "摇一摇" ) );
        llMenu.addView( createIndicatorViewLayer1( "热门推荐" ) );
        llMenu.addView( createIndicatorViewLayer2( "餐饮美食" ) );
        llMenu.addView( createIndicatorViewLayer2( "休闲娱乐" ) );
        llMenu.addView( createIndicatorViewLayer2( "生活服务" ) );
        llMenu.addView( createIndicatorViewLayer2( "商场超市" ) );
        llMenu.addView( createIndicatorViewUserInfo() );
    }

    private View createIndicatorViewLayer1( String title )
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View tabIndicator = inflater.inflate( R.layout.navi_tab_ind_layer_1, llMenu, false );
        final TextView titleView = (TextView) tabIndicator.findViewById( R.id.navi_bar_ind_title );
        titleView.setText( title );
        return tabIndicator;
    }

    private View createIndicatorViewLayer2( String title )
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View tabIndicator = inflater.inflate( R.layout.navi_tab_ind_layer_2, llMenu, false );
        final TextView titleView = (TextView) tabIndicator.findViewById( R.id.navi_bar_ind_title );
        titleView.setText( title );
        return tabIndicator;
    }

    private View createIndicatorViewUserInfo()
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View tabIndicator = null;
        // check whether use has logged in
        SharedPreferences sharedPref = getSharedPreferences( "userInfo", Context.MODE_PRIVATE );
        if( sharedPref.getBoolean( SharedPrefConst.SHARED_PREF_BOOL_USER_IS_LOGIN, false ) )
        {
            tabIndicator = inflater.inflate( R.layout.navi_tab_ind_login, llMenu, false );
            //            tvUserName.setText( sharedPref.getString( MemberCenterActivity.SHARED_PREF_STR_LOGIN_USER_NAME, "" ) );
            //            tvCredits.setText( sharedPref.getString( MemberCenterActivity.SHARED_PREF_STR_CREDITS, "" ) );
        }
        else
        {
            tabIndicator = inflater.inflate( R.layout.navi_tab_ind_logout, llMenu, false );
        }

        return tabIndicator;
    }

    protected View createView( int viewId, ViewGroup parent )
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View tabIndicator = inflater.inflate( viewId, parent, false );
        return tabIndicator;
    }

    public void navigationBar( View view )
    {
        Log.d( "AbstractSlideMenuActivity", "press navigationBar" );
        slideToRight( ( (AbsoluteLayout.LayoutParams) llMain.getLayoutParams() ).x );
    }

    @Override
    public boolean onTouchEvent( MotionEvent event )
    {
        Log.d( "AbstractSlideMenuActivity", "x postion: " + (int) event.getX() );
        if( event.getAction() == MotionEvent.ACTION_DOWN )
        {
            p1 = (int) event.getX();
        }

        if( event.getAction() == MotionEvent.ACTION_MOVE )
        {
            p2 = (int) event.getX();
            d = p2 - p1;
            p1 = p2;

            int tempX = ( (AbsoluteLayout.LayoutParams) llMain.getLayoutParams() ).x;
            tempX += d;

            if( tempX >= WIDTH )
            {
                tempX = WIDTH;
            }
            if( tempX <= 0 )
            {
                tempX = 0;
            }

            llMain.setLayoutParams( new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.MATCH_PARENT,
                AbsoluteLayout.LayoutParams.MATCH_PARENT, tempX, 0 ) );
        }

        if( event.getAction() == MotionEvent.ACTION_UP )
        {
            slideIfNecessary();
        }
        return super.onTouchEvent( event );
    }

    private void slideIfNecessary()
    {
        final int x = ( (AbsoluteLayout.LayoutParams) llMain.getLayoutParams() ).x;

        if( x >= ( WIDTH / 2 ) )
        {
            slideToRight( x );
        }
        else
        {
            new Thread()
            {
                public void run()
                {
                    for( int i = x; i > 0; )
                    {
                        i -= 5;
                        Message msg = updateHandler.obtainMessage();
                        if( i < 0 )
                            i = 0;
                        msg.arg1 = i;
                        updateHandler.sendMessage( msg );
                        try
                        {
                            Thread.sleep( 5 );
                        }
                        catch( InterruptedException e )
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }

    }

    protected void slideToRight( final int x )
    {
        new Thread()
        {
            public void run()
            {
                for( int i = x; i < WIDTH; )
                {
                    i += 5;
                    Message msg = updateHandler.obtainMessage();
                    if( i > WIDTH )
                        i = WIDTH;
                    msg.arg1 = i;
                    updateHandler.sendMessage( msg );
                    try
                    {
                        Thread.sleep( 5 );
                    }
                    catch( InterruptedException e )
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    Handler updateHandler = new Handler()
    {
        public void handleMessage( android.os.Message msg )
        {
            llMain.setLayoutParams( new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.MATCH_PARENT,
                AbsoluteLayout.LayoutParams.MATCH_PARENT, msg.arg1, 0 ) );
        }
    };

}
